#pragma once
#include "dbng.hpp"
#include "../../DB/DBManager.h"
#include "../Common/Common.h"

#include "include\wrapper\cef_message_router.h"
#include "include/cef_parser.h"
#include "iguana\json.hpp"

#include <string>
#include <vector>
#include <memory>
using namespace std;
using namespace ormpp;
using namespace iguana;

struct Department{
    int id;
    int parentid;
    string name;
};
REFLECTION(Department, id, parentid, name)

class DepartmentManager:public CefRefCount
{
private:
    static CefRefPtr<DepartmentManager> department;
public:
    DepartmentManager(){}
    ~DepartmentManager(){}

    static bool process(string action, CefRefPtr<CefDictionaryValue> data,CefRefPtr<CefBrowser> browser,
        CefRefPtr<CefMessageRouterBrowserSide::Callback> callback){
        if(action == "add"){
            Department department=CefDictionary2Struct<Department>(data);
            DBManager::inst()->insert(department);
            callback->Success("{}");
        }else if(action == "add_sub"){
            callback->Success("{}");           
        }else if(action == "del"){
            Department department=CefDictionary2Struct<Department>(data);
            DBManager::inst()->delete_records<Department>("name='"+department.name+"'");
            callback->Success("{}");
        }else if(action == "query"){
            iguana::string_stream ss;
            vector<Department> item = DBManager::inst()->query<Department>();
            iguana::json::to_json(ss, item);
            callback->Success(ss.str());
        }
        else if(action == "del"){
            callback->Success("{}");           
        }
        return false;
    }
};
